package com.android.hesapci;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: dzhoshkun
 * Date: 12/1/11
 * Time: 12:07 AM
 * To change this template use File | Settings | File Templates.
 */
public class NewUserActivity extends Activity {
    private EditText nameEditText;
    private DatePicker dateReachedResponsibility;
    private DatePicker dateStartedPrayer;
    private DatePicker dateStartedFasting;
    private Button addUser;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newuser);

        this.nameEditText = (EditText) findViewById(R.id.name);
        this.nameEditText.setOnKeyListener(new NameOnKeyListener());
        this.addUser = (Button) findViewById(R.id.adduser);
        this.addUser.setClickable(false);
        this.addUser.setOnClickListener(new AddUserOnClickListener(this));
        this.dateReachedResponsibility = (DatePicker) findViewById(R.id.datereachedresp);
        this.dateStartedPrayer = (DatePicker) findViewById(R.id.datestartedprayer);
        this.dateStartedFasting = (DatePicker) findViewById(R.id.datestartedfasting);
    }

    public class NameOnKeyListener implements View.OnKeyListener {
        public boolean onKey(View v, int key, KeyEvent e) {
            if (e.getAction()==KeyEvent.ACTION_DOWN && key==KeyEvent.KEYCODE_ENTER) {
                if (NewUserActivity.this.nameEditText.getText().length()>5)
                    NewUserActivity.this.addUser.setClickable(true);
                else
                    NewUserActivity.this.addUser.setClickable(false);

                return true;
            }
            else
                return false;
        }
    }

    public class AddUserOnClickListener implements View.OnClickListener {
        private NewUserActivity newUserActivity;

        public AddUserOnClickListener(NewUserActivity newUserActivity) {
            this.newUserActivity = newUserActivity;
        }

        public void onClick(View v) {
            if (NewUserActivity.this.addUser.isClickable()) {
                String name = NewUserActivity.this.nameEditText.getText().toString();
                Date dateReachedResponsibility = new Date(NewUserActivity.this.dateReachedResponsibility.getYear()-UserData.YEAR_OFFSET,
                                                            NewUserActivity.this.dateReachedResponsibility.getMonth()-1,
                                                            NewUserActivity.this.dateReachedResponsibility.getDayOfMonth());
                Date dateStartedPrayer = new Date(NewUserActivity.this.dateStartedPrayer.getYear()-UserData.YEAR_OFFSET,
                                                    NewUserActivity.this.dateStartedPrayer.getMonth()-1,
                                                    NewUserActivity.this.dateStartedPrayer.getDayOfMonth());
                Date dateStartedFasting = new Date(NewUserActivity.this.dateStartedFasting.getYear()-UserData.YEAR_OFFSET,
                                                    0,
                                                    0);
                try {
                    UserData newUser = new UserData(name, dateReachedResponsibility, dateStartedPrayer, dateStartedFasting);
                    Bundle newUserBundle = new Bundle();
                    newUserBundle.putSerializable(Hesapci2.KEY_USER_DATA, newUser);
                    Intent data = new Intent();
                    data.putExtras(newUserBundle);
                    this.newUserActivity.setResult(Hesapci2.REQUEST_CODE, data);
                    this.newUserActivity.finish();
                }
                catch (InvalidTimeDifferenceException e) {
                    // TODO: warning!
                }
            }
        }
    }
}
